home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagn_r.zip / OOP.SWG / 0058_TV Pick list example.pas < prev   
Pascal/Delphi Source File  |  1995-02-28  |  4KB  |  163 lines

  1. {
  2. The following example is to demonstrate the use of a Pick List.
  3. This Pick List will read a line of data and validate the field
  4. as well as keep a history of valid commands.
  5. }
  6. {$X+}
  7. Program PickListExample;
  8.  
  9. uses Objects, App, Menus, Drivers, Views, Dialogs, MsgBox;
  10.  
  11. type
  12.   YourKeySet = set of char;
  13.  
  14. const
  15.    cmMakeDialog = 101;
  16.    ValidSet  : YourKeySet = [#0..#31,'0'..'9'];
  17.  
  18. type
  19.  
  20.   PFInputLine = ^TFInputLine;
  21.   TFInputLine = object(TInputLine)
  22.     ValidKeys : YourKeySet;
  23.     constructor Init(var Bounds: TRect; AMaxLen: integer;
  24.                      ChrSet: YourKeySet);
  25.     procedure HandleEvent(var Event: TEvent); virtual;
  26.     procedure GetData(var Rec); virtual;
  27.     procedure SetData(var Rec); virtual;
  28.     function DataSize: word; virtual;
  29.   end;
  30.  
  31. PMyApp = ^TMyApp;
  32. TMyApp = object(TApplication)
  33.   procedure InitMenuBar;virtual;
  34.   procedure InitStatusLine;virtual;
  35.   procedure MakeDialog;
  36.   procedure HandleEvent(var Event: TEvent);virtual;
  37.  end;
  38.  
  39. constructor TFInputLine.Init(var Bounds: TRect; AMaxLen: integer;
  40.                              ChrSet: YourKeySet);
  41. begin
  42.   TInputLine.Init(Bounds,AMaxLen);
  43.   ValidKeys:= ChrSet;
  44. end;
  45.  
  46. procedure TFInputLine.HandleEvent(var Event: TEvent);
  47. var
  48.   Number : longint;
  49.   Code : integer;
  50. begin
  51.   case Event.What of
  52.     evKeyDown :
  53.       begin
  54.         if not(Event.CharCode in ValidKeys) then
  55.           ClearEvent(Event)
  56.          else
  57.            if Data^ <> '' then
  58.              begin
  59.                val(Data^, Number, Code);
  60.                if (Code <> 0) or (Number < 0) or (Number > 65535) then
  61.                  begin
  62.                    Data^ := '';
  63.                    MessageBox('Valid number is 0 to 65536.',nil,mfOkButton);
  64.                    ClearEvent(Event);
  65.                  end;
  66.              end;
  67.       end;
  68.   end;
  69.   TInputLine.HandleEvent(Event);
  70. end;
  71.  
  72. procedure TFInputLine.GetData(var Rec);
  73. var
  74.   Code : integer;
  75. begin
  76.   Val(Data^,word(Rec), Code);
  77. end;
  78.  
  79. procedure TFInputLine.SetData(var Rec);
  80. begin
  81.   Str(word(Rec),Data^);
  82. end;
  83.  
  84. function TFInputLine.DataSize: word;
  85. begin
  86.   DataSize := SizeOf(word);
  87. end;
  88.  
  89. procedure TMyApp.InitMenuBar;
  90.   var R: TRect;
  91. begin
  92.   GetExtent(R);
  93.   R.B.Y := R.A.Y + 1;
  94.   MenuBar := New(PMenuBar, Init(R, NewMenu(
  95.     NewSubMenu('~T~est', hcNoContext, NewMenu(
  96.       NewItem('~D~ialog','F2',kbF2,cmMakeDialog,hcNoContext,
  97.       NewLine(
  98.       NewItem('E~x~it', 'Alt-X', kbAltX, cmQuit, hcNoContext,
  99.       nil)))),nil))));
  100. end;
  101.  
  102. procedure TMyApp.MakeDialog;
  103. var
  104.  R : TRect;
  105.  Dialog : PDialog;
  106.  D : word;
  107.  InputLine : PInputLine;
  108.  Lable,Hist : PView;
  109. begin
  110.  GetExtent(R);
  111.  R.Assign(10,5,60,15);
  112.  Dialog := New(PDialog,Init(R,'Demo Dialog'));
  113.  With Dialog^ do
  114.    begin
  115.      R.Assign(5,4,20,5);
  116.      InputLine := New(PFInputLine,Init(R,15,ValidSet));
  117.      Insert(InputLine);
  118.      R.Assign(5,3,20,4);
  119.      Lable := New(PLabel,Init(R,'InputLine',Inputline));
  120.      Insert(Lable);
  121.      R.Assign(21,4,23,5);
  122.      Hist := New(PHistory,Init(R,InputLine,0));
  123.      Insert(Hist);
  124.    end;
  125.  D := Desktop^.ExecView(Dialog);
  126.  Dispose(Dialog,Done);
  127. end;
  128.  
  129. procedure TMyApp.InitStatusLine;
  130. var
  131.  R : TRect;
  132. begin
  133.  GetExtent(R);
  134.  R.A.Y := R.B.Y - 1;
  135.  StatusLine :=  New(PStatusLine,Init(R,
  136.                   NewStatusDef(0,$FFFF,
  137.                     NewStatusKey('~A~lt-X',kbAltX,cmQuit,
  138.                 nil),nil)));
  139. end;
  140.  
  141. procedure TMyApp.HandleEvent(var Event: TEvent);
  142. begin
  143.   TApplication.HandleEvent(Event);
  144.   if Event.What = evCommand then
  145.   begin
  146.     case Event.Command of
  147.       cmMakeDialog: MakeDialog;
  148.     else
  149.       Exit;
  150.     end;
  151.     ClearEvent(Event);
  152.   end;
  153. end;
  154.  
  155. var
  156.  MyApp: TMyApp;
  157.  
  158. begin
  159.  MyApp.Init;
  160.  MyApp.Run;
  161.  MyApp.Done;
  162. end.
  163.